﻿namespace BulkListActions
{
    using System;
    using System.Web.UI.WebControls;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.WebControls;
    using Microsoft.SharePoint.WebPartPages;
    using Microsoft.SharePoint.Utilities;
    using System.Web;

    public class ListActionsMenuItems : WebControl
    {
        protected override void CreateChildControls()
        {
            try
            {
                ListViewWebPart parentListView = Helper.FindListView(this.Parent) as ListViewWebPart;

                if (parentListView != null)
                {
                    Guid guid = new Guid(parentListView.ListName);
                    SPList list = SPContext.Current.Web.Lists[guid];
                    
                    if (list.TemplateFeatureId == new Guid("00bfea71-e717-4e80-aa17-d0c71b360101"))
                    {
                        if (!this.ChildControlsCreated)
                        {
                            base.CreateChildControls();

                            // Create sub menu
                            SubMenuTemplate child = new SubMenuTemplate();
                            child.Text = "Bulk Actions";
                            child.ImageUrl = "~site/_layouts/images/actions.jpg";

                            // Delete
                            DeleteItemsAction delete = new DeleteItemsAction();

                            //PostBackMenuItemTemplate mytestmenu = new PostBackMenuItemTemplate();
                            //mytestmenu.ID = "mytestmenu";
                            //mytestmenu.Text = "Test Delete";
                            //mytestmenu.Description = "Test Description";
                            //mytestmenu.ClientOnClickPostBackConfirmation = "You will delete selected items, are you sure ?";
                            //mytestmenu.OnPostBack += new EventHandler<EventArgs>(mnuListItem_OnPostBack);


                            // Check Out
                            CheckOutItemsAction co = new CheckOutItemsAction();
                            // Check In
                            CheckInItemsAction ci = new CheckInItemsAction();
                            // Publish
                            PublishItemsAction publish = new PublishItemsAction();
                            // Unpublish
                            UnpublishItemsAction unpublish = new UnpublishItemsAction();
                            // Update Metadata
                            UpdateMetaDataAction metadata = new UpdateMetaDataAction();
                            // Move
                            MoveItemsAction move = new MoveItemsAction();

                            //Separator
                            MenuSeparatorTemplate separator = new MenuSeparatorTemplate();

                            // Add bulk actions to sub menu
                            // child.Controls.Add(mytestmenu);
                            child.Controls.Add(delete);
                            child.Controls.Add(co);
                            child.Controls.Add(ci);
                            child.Controls.Add(publish);
                            child.Controls.Add(unpublish);
                            child.Controls.Add(separator);
                            child.Controls.Add(metadata);
                            child.Controls.Add(move);

                            // Add sub menu to Actions menu
                            this.Controls.Add(child);
                        }
                    }
                    
                }
                //base.CreateChildControls();
            }
            catch
            {
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            this.EnsureChildControls();
            base.OnLoad(e);
        }

        void mnuListItem_OnPostBack(object sender, EventArgs e)
        {
            SPUtility.Redirect("settings.aspx", SPRedirectFlags.Static | SPRedirectFlags.RelativeToLayoutsPage, HttpContext.Current);
        }

    }
}

